#include "tools.h"

void get_char(void)
{
	cout << "****[q]返回****" << endl;
	while('q' != getch());
	return;
}

unsigned int generator_BookId()
{
	int idfd = open("../data/id.dat",O_RDWR | O_EXCL , 0644);
	if(idfd < 0)
	{
		idfd = open("../data/id.dat",O_RDWR | O_CREAT , 0644);
		int id[4] = {100000,10000,100,1000000};
		write(idfd,&id,sizeof(id));
		close(idfd);
		return 100000;
	}
	int id[4] = {};
	read(idfd,&id,sizeof(id));
	++id[0];
	lseek(idfd,0,SEEK_SET);
	write(idfd,&id,sizeof(id));
	close(idfd);
	return id[0];
}

unsigned int generator_BookListId()
{
	int idfd = open("../data/id.dat",O_RDWR | O_EXCL , 0644);
	if(idfd < 0)
	{
		idfd = open("../data/id.dat",O_RDWR | O_CREAT , 0644);
		int id[4] = {100000,10000,100,1000000};
		write(idfd,&id,sizeof(id));
		close(idfd);
		return 10000;
	}
	int id[4] = {};
	read(idfd,&id,sizeof(id));
	++id[1];
	lseek(idfd,0,SEEK_SET);
	write(idfd,&id,sizeof(id));
	close(idfd);
	return id[1];
}

unsigned int generator_ManagerId()
{
	int idfd = open("../data/id.dat",O_RDWR | O_EXCL , 0644);
	if(idfd < 0)
	{
		idfd = open("../data/id.dat",O_RDWR | O_CREAT , 0644);
		int id[4] = {100000,10000,100,1000000};
		write(idfd,&id,sizeof(id));
		close(idfd);
		return 100;
	}
	int id[4] = {};
	read(idfd,&id,sizeof(id));
	++id[2];
	lseek(idfd,0,SEEK_SET);
	write(idfd,&id,sizeof(id));
	close(idfd);
	return id[2];
}
unsigned int generator_UserId()
{
	int idfd = open("../data/id.dat",O_RDWR | O_EXCL , 0644);
	if(idfd < 0)
	{
		idfd = open("../data/id.dat",O_RDWR | O_CREAT , 0644);
		int id[4] = {100000,10000,100,1000000};
		write(idfd,&id,sizeof(id));
		close(idfd);
		return 1000000;
	}
	int id[4] = {};
	read(idfd,&id,sizeof(id));
	++id[3];
	lseek(idfd,0,SEEK_SET);
	write(idfd,&id,sizeof(id));
	close(idfd);
	return id[3];
}

